# pragma warning (disable:4819)


#ifndef PLAYERWIDGET_H
#define PLAYERWIDGET_H

#include <QVideoWidget>
#include "SDL2/SDL.h"
#include <QMutex>
#include <qtimer.h>
#include <QMap>
#include "SDL2/SDL_test_font.h"

#include "ring_buffer_s.h"

#include "RateCount.h"


#define MAX_WIDTH 3840
#define MAX_HEIGHT 2160
const QString info_resolution= "info_resolution";
const QString info_userid= "info_userid";
//const QString info_byterate= "info_byterate";
const QString info_framerate= "info_framerate";
class PlayerWidget : public QVideoWidget
{
    Q_OBJECT

public:
    explicit PlayerWidget(QWidget *parent = nullptr);
    ~PlayerWidget();


    void SetWindow(SDL_Window * ref);
    void SetRenderer(SDL_Renderer * ref);

    SDL_Window * GetWindow();
    SDL_Renderer * GetRenderer();


    void OnGotVideoFrame(const char *y, size_t y_size,
                                 const char *u, size_t u_size,
                                 const char *v, size_t v_size,
                                 uint64_t timestamp,uint32_t w,uint32_t h);

    void OnGotAudioFrame(const char *pcm,
                         uint32_t bitsPerSample,
                         uint32_t sampleRate,
                         uint32_t numberOfChannels,
                         int64_t numberofFrames,
                         int64_t timestamp);


//    void StartRenderTimer();
//    void FillTeztYuvImage(int nWidth, int nHeight);
    void CheckNeetReSDLInit(int w,int h);



   public:


    void  AddInfoMap(const QString  & key,const QString & value);
    static void  fillaudioup(void *user_data, Uint8 * stream, int len);



    /**
    *  获取音频数据
    * @param stream 提供的音频缓冲区
    * @param len  提供的音频缓冲区的长度
    * *@reurn  当数据不够的时候,返回值可能会小于len
    *          其他情况下,=len
    */
    int ExtractAudioData(Uint8 * stream, int len);



    int PushinAudioData(char *audioData,
                        size_t sampleRate,
                        size_t bytesPerSample,
                        size_t channels,
                        size_t frameSize);

    int PushinAudioData(const char *pcm,
                        uint32_t bitsPerSample,
                        uint32_t sampleRate,
                        uint32_t numberOfChannels,
                        int64_t numberofFrames,
                        int64_t timestamp);


    void Reset();


signals:
    void Sig_FrameSizeChanged(int w,int h);
    void Sig_NewFrame(int w,int h);
protected:
    void keyPressEvent(QKeyEvent *event) override;
    void mouseDoubleClickEvent(QMouseEvent *event) override;
    void mousePressEvent(QMouseEvent *event) override;
private:
    void    close_audio(SDL_AudioDeviceID & audiodevice);

    void    open_audio(SDL_AudioDeviceID & audiodevice,SDL_AudioSpec & spec);

    void    reopen_audio(SDL_AudioDeviceID & audiodevice,SDL_AudioSpec & spec);

    void    CheckNeetReAudioInit(SDL_AudioSpec & spec,int sampleRate,int bytesPerSample,int channels);
public:

      uint8_t _frameBuffer[MAX_WIDTH*MAX_HEIGHT*2];
      int     _stride[3];
      volatile int     _frame_width;
      volatile int     _frame_height;
      QMutex  _frameBufLock;
private:
    QWidget *                   MainWindowWidget;

    SDL_Window *                WindowRef;
    SDL_Renderer *              RendererRef;
    SDL_Texture*                textureRef;
    QTimer *                    Time;
    SDL_AudioDeviceID           _audiodevice;
    SDL_AudioSpec               _audiospec;

    int                         position;
    ring_buffer_s               _audio_buffer;
    QMap<QString,QString>       _infomap;
    QString                     _infostr;

    RateCount *                 _framecount;
//    RateCount *                 _byteratevideocount;
//    RateCount *                 _byterateaudiocount;
private slots:

    void Slot_Render(int w,int h);
    void Slot_SDLInit(int w,int h);
    void updateRateCount();
};

#endif // PLAYERWIDGET_H

